import 'dart:async';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:shishi/Colors.dart';
import 'package:shishi/bean/kefubean.dart';
import 'package:shared_preferences/shared_preferences.dart';

class KeFu extends StatefulWidget {
  @override
  _KeFuState createState() => _KeFuState();
}

class _KeFuState extends State<KeFu> {
  SharedPreferences sharedPreferences;
  ScrollController _scrollController = new ScrollController();
  TextEditingController _textEditingController = new TextEditingController();
  FocusNode _contentFocusNode = FocusNode();

  String question = 'robot';

  KeFus _keFu;
  List<kefus> _list = new List<kefus>();
  List _listtext = new List();       //添加文字

  String userid;

  Future<String> _GetUserInfo() async {
    sharedPreferences = await SharedPreferences.getInstance();
    setState(() {
      userid = sharedPreferences.getString("phone");
    });
  }

  void GetKefu() async {
    try {
      Response response = await Dio().get(
          "http://api.tianapi.com/txapi/robot/index?key=a69400da230d90d3d4ed644b6d3d1fb7&question=$question&userid=$userid"
      );
      _keFu = new KeFus(response.data);
      _list.addAll(_keFu.newslist);

      setState(() {
        Timer(Duration(milliseconds: 100), () {
          _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
        });
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _GetUserInfo();
    Timer(Duration(milliseconds: 100), () {
      _listtext.add('我可以问你一些问题吗');
      GetKefu();
      setState(() {

      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('智能客服'),
      ),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          children: <Widget>[
            Expanded(
              child: ListView.builder(
                controller: _scrollController,
                itemCount: _list.length,
                itemBuilder: (context, index) {
                  return Column(
                    children: <Widget>[
                      ListTile(
                        trailing: Container(
                          padding: EdgeInsets.all(5),
                          child: Text('${_listtext[index]}',style: TextStyle(
                              color: Colors.white, fontWeight: FontWeight.w900)),
                          decoration: BoxDecoration(
                              color: Cos.zhutise,
                              borderRadius: BorderRadius.circular(5)
                          ),
                        ),
                      ),
                      ListTile(
                        leading: ClipOval(
                          child: Image.network(
                            'https://scpic.chinaz.net/files/pic/pic9/201803/zzpic11109.jpg',
                            width: 30,
                            height: 30,
                            fit: BoxFit.cover,
                          ),
                        ),
                        title: Container(
                          padding: EdgeInsets.all(5),
                          child: Text('${_list[index].reply}',style: TextStyle(
                              color: Cos.zhutise, fontWeight: FontWeight.w900)),
                          decoration: BoxDecoration(
                              color: Colors.green[100],
                              borderRadius: BorderRadius.circular(5)
                          ),
                        ),
                      ),
                    ],
                  );
                },
              ),
            ),
            Container(
              child: Row(
                children: <Widget>[
                  Container(
                    width: MediaQuery.of(context).size.width * 0.8,
                    child: TextField(
                      controller: _textEditingController,
                      onChanged: (text) {
                        question = text;
                      },
                      focusNode: _contentFocusNode,
                      textInputAction: TextInputAction.search,
                      decoration: InputDecoration(
                          hintText: '请输入内容',
                          suffixIcon: IconButton(
                            icon: Icon(Icons.close),
                            onPressed: () {
                              _textEditingController.clear();
                            },
                          )),
                    ),
                  ),
                  Expanded(
                    child: IconButton(
                      icon: Icon(Icons.near_me,color: Cos.zhutise,),
                      onPressed: () {
                        GetKefu();
                        _listtext.add(question);
                      },
                    ),
                  )
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
